﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using TimberSmart.Data.TestData;
using TimberSmart.Model.Business;
using TimberSmart.Model.Security;
using TimberSmart.Model.Common;

namespace TimberSmart.Data
{
    public class TimberSmartDbContext : DbContext
    {
        // Connection to TSMEL02's SQL test account, TimberSmartOTS. We're going to inject this from elsewhere eventually.        
        private static string _connectionString =
            "Data Source=timbersmart.ddns.net,1433;" +
            "Initial Catalog=TimberSmartOTS;" + 
            "Persist Security Info=True;" + 
            "User ID=timbersmart;" + 
            "Password=T1mberSm@rt";

        public TimberSmartDbContext() : base(nameOrConnectionString: _connectionString) { }

        // DbSet's
        public DbSet<User> Users { get; set; }

        // This is where you specify what you want to happen when you change the model.
        // At the moment it's set to drop the database whenever the model changes and recreate it.
        // It's also where you can add 'seed' data to the database after it's recreated.
        static TimberSmartDbContext()
        {
            Database.SetInitializer(new TimberSmartDatabaseInitializer());            
        }

        // This is where you can do your customizations to the model before it's published.
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            // This prevents EF from creating a table called 'Customers' instead of 'Customer'
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();


            // Turn off lazy loading, otherwise we'll send giant objects.
            this.Configuration.LazyLoadingEnabled = false;


            //  This handles lazy loading and change tracking, we'll manage that.
            this.Configuration.ProxyCreationEnabled = false;


            // Our service layer will handle our validation. I hope ;D
            this.Configuration.ValidateOnSaveEnabled = false;


            // Adding configurations for each entity, see Configuration folder for examples.
            //modelBuilder.Configurations.Add(new CustomerConfiguration());
            //modelBuilder.Configurations.Add(new AddressConfiguration());
            //modelBuilder.Configurations.Add(new UserConfiguration());
        }
    }
}

// Exception: Cannot drop database "TimberSmartOTS" because it is currently in use.
// You have the database open in connection manager probably.